<?php
/**
 * @Author: anchen
 * @Date:   2017-05-23 18:37:59
 * @Last Modified by:   anchen
 * @Last Modified time: 2017-05-26 19:44:43
 */
//拒绝非法访问
defined('ACCESS_KEY9296')||exit('denied');

//日志
//
//传一个内容,判断当前日志的大小,大于1m备份否则写入

class log{

    const LOGFILE = 'curr.log';//建一个常量,日志的名称
    //写日志
    public static function write($cont){
        $cont .="\r\n";
        $log = self::isBak();
        $fh = fopen($log, 'ab');
        fwrite($fh,$cont);
        fclose($fh);
    }

    //备份日志
    public static function bak(){
        //把原来日志文件改名
    $log = ROOT . 'data/log/'.self::LOGFILE;
    $bak = ROOT . 'data/log/'.date("Y-m-d").'-'.time().'-'.mt_rand(10000,99999).'.bak';
    return rename($log,$bak);
    }

    //读取并判断日志的大小
    
    public static function isBak(){
        $log = ROOT . 'data/log/'.self::LOGFILE;
        if(!file_exists($log)){
            touch($log);
            return $log;
        }
        else{
            clearstatcache();
            $size = filesize($log);
            if($size <= 1024*1024){ //
                return $log;
            }
            if(!self::bak()){  //备份失败则继续放原文件写入日志
                return $log;
            }
            else{
            touch($log);
            return $log;
            }
        }
    }
}